package com.c.ctgproject.ui.vehicleadmission

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.c.ctgproject.mode.carEntrance
import com.c.ctgproject.retrofit.HttpHelper
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.retrofit.Service
import com.c.ctgproject.utils.handlerNull
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.MultipartBody

class VehicleAdmissionViewModel : ViewModel() {
    //车辆入场回调
    private val responsedata: MutableLiveData<Response<*>> = MutableLiveData()
    private val carReturndata: MutableLiveData<Response<*>> = MutableLiveData()

    //车辆入场信息
    private val postcarEntrancedata: MutableLiveData<Response<carEntrance>> = MutableLiveData()
    private val serviece = HttpHelper.getInstance().create(Service::class.java)

    private val mError = MutableLiveData<String>()
    val error: LiveData<String> get() = mError

    fun getresponsedata(): MutableLiveData<Response<*>> {
        return responsedata
    }

    fun getcarReturndata(): MutableLiveData<Response<*>> {
        return carReturndata
    }

    fun getpostcarEntrancedata(): MutableLiveData<Response<carEntrance>> {
        return postcarEntrancedata
    }

    //车辆入库
    fun EntranceCar(token: String, parts: ArrayList<MultipartBody.Part>) {
        serviece.carEntrance(token, parts)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<*>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    e.printStackTrace()
                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(userBeanResponse: Response<*>) {
                    responsedata.value = userBeanResponse
                }
            })
    }

    //车辆初检
    fun postCarEntrance(token: String, carId: String) {
        serviece.postcarEntrance(token, carId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<carEntrance>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    mError.value=e.message.handlerNull()
                    e.printStackTrace()
                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(userBeanResponse: Response<carEntrance>) {
                    postcarEntrancedata.value = userBeanResponse
                }
            })

    }

    //退车
    fun carReturn(token: String, carId: String, remark: String) {
        serviece.carReturn(token, carId, remark)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<Response<*>> {
                override fun onError(e: Throwable) {
                    Log.d("错误信息", "" + e.message)
                    mError.value=e.message.handlerNull()
                    e.printStackTrace()

                }

                override fun onComplete() {}
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(userBeanResponse: Response<*>) {
                    carReturndata.value = userBeanResponse
                }
            })

    }
}